<?php
class GeneralHelper extends AppHelper{
    
    public $helpers = array ('Session'); 
    function base_url()
    {
        return Router::url('/', true);
    }
    
    
    function unicode_convert($str){
    	if(!$str) return false;
    	$unicode = array(
    				'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),
    				'A'=>array('Á','À','Ả','Ã','Ạ','Ă','Ắ','Ặ','Ằ','Ẳ','Ẵ','Â','Ấ','Ầ','Ẩ','Ẫ','Ậ'),
    				'd'=>array('đ'),
    				'D'=>array('Đ'),
    				'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),
    				'E'=>array('É','È','Ẻ','Ẽ','Ẹ','Ê','Ế','Ề','Ể','Ễ','Ệ'),
    				'i'=>array('í','ì','ỉ','ĩ','ị'),
    				'I'=>array('Í','Ì','Ỉ','Ĩ','Ị'),
    				'o'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','õ','ớ','ờ','ở','ỡ','ợ'),
    				'O'=>array('Ó','Ò','Ỏ','Õ','Ọ','Ô','Ố','Ồ','Ổ','Ỗ','Ộ','Õ','Ớ','Ờ','Ở','Ỡ','Ợ'),
    				'u'=>array('ú','ù','ủ','ũ','ụ','ý','ứ','ừ','ử','ữ','ự'),
    				'U'=>array('Ú','Ù','Ủ','Ũ','Ụ','Ý','Ứ','Ừ','Ử','Ữ','Ự'),
    				'y'=>array('ý','ỳ','ỷ','ỹ','ỵ'),
    				'Y'=>array('Ý','Ỳ','Ỷ','Ỹ','Ỵ'),
    				'-'=>array(' ','.','/',"'",'`','(',')',',','!','"','"','"','%','&','@','#','$'),
    				);
    	foreach($unicode as $nonUnicode=>$uni){
    		foreach($uni as $value)
    		$str = str_replace($value,$nonUnicode,$str);
    	}
        $str = Inflector::slug($str, '-');
    	$str = strtolower($str);
    	return $str;
    }
    
 
    function pagination($pageDisplay, $numPage, $link, $currentPage)
	{
	    if($numPage == 1)
            return;
		$html_page = "";
		$html_page .= "<div class='pagination paging'><ul class='ul-pagination'>";
		if($numPage <= $pageDisplay)
		{

            if($currentPage > 1)
                $linkPage = str_replace('np',($currentPage-1),$link);
            else
                $linkPage = 'javascript:void(0)';
            
		    $html_page .= "<li class=''><a href='$linkPage' class='prev'></a></li>";
			for($i=1;$i<=$numPage;$i++)
			{
				$pageActive = ($currentPage == $i) ? "class='active'" : "";
				$linkPage = str_replace('np',$i,$link);
				$html_page .= "<li $pageActive><a href='$linkPage'>$i</a></li>";
			}
            if($currentPage <= $numPage - 1)
                $linkPage = str_replace('np',($currentPage+1),$link);
            else
                $linkPage = 'javascript:void(0)';
			$html_page .= "<li><a href='$linkPage' class='next'></a></li>";
		}
		else
		{
			if($currentPage <= ceil($pageDisplay/2))
			{
				//$html_page .= "<li class='pageDisable'><a href=''>First</a></li>";
				$html_page .= "<li class='pageDisable'><a href='' class='prev'></a></li>";
				for($i=1; $i<=$pageDisplay; $i++)
				{
					$pageActive = ($currentPage == $i) ? "class='active'" : "";
					$linkPage = str_replace('np',$i,$link);
					$html_page .= "<li $pageActive><a href='$linkPage'>$i</a></li>";
				}
				if($pageDisplay < $numPage) $html_page .= "<li class='pageSeparator'><a href=''>...</a></li>";
				$linkPage = str_replace('np',($currentPage+1),$link);
				$html_page .= "<li><a href='$linkPage' class='next'></a></li>";
				//$linkPage = str_replace('np',$numPage,$link);
				//$html_page .= "<li><a href='$linkPage'>Last</a></li>";
			}
			if($currentPage > ceil($pageDisplay/2) && $currentPage <= ($numPage - ceil($pageDisplay/2)))
			{
				//$linkPage = str_replace('np',1,$link);
				//$html_page .= "<li><a href='$linkPage'>First</a></li>";
				$linkPage = str_replace('np',($currentPage-1),$link);
				$html_page .= "<li><a href='$linkPage' class='prev'></a></li>";
				$html_page .= "<li class='pageSeparator'><a href=''>...</a></li>";
				for($i=($currentPage-floor($pageDisplay/2)); $i<= min($numPage,($currentPage+floor($pageDisplay/2))); $i++)
				{
					$pageActive = ($currentPage == $i) ? "class='active'" : "";
					$linkPage = str_replace('np',$i,$link);
					$html_page .= "<li $pageActive><a href='$linkPage'>$i</a></li>";
				}
				$html_page .= "<li class='pageSeparator'><a href=''>...</a></li>";
				
				$linkPage = str_replace('np',($currentPage+1),$link);
				$html_page .= "<li><a href='$linkPage' class='next'></a></li>";
				//$linkPage = str_replace('np',$numPage,$link);
				//$html_page .= "<li><a href='$linkPage'>Last</a></li>";
			}
			if($currentPage > ($numPage - ceil($pageDisplay/2)))
			{
				//$linkPage = str_replace('np',1,$link);
				//$html_page .= "<li><a href='$linkPage'>First</a></li>";
				$linkPage = str_replace('np',($currentPage-1),$link);
				$html_page .= "<li><a href='$linkPage' class='prev'></a></li>";
				$html_page .= "<li class='pageSeparator'><a href=''>...</a></li>";
				for($i=($currentPage-floor($pageDisplay/2)); $i<= min($numPage,($currentPage+floor($pageDisplay/2))); $i++)
				{
					$pageActive = ($currentPage == $i) ? "class='active'" : "";
					$linkPage = str_replace('np',$i,$link);
					$html_page .= "<li $pageActive><a href='$linkPage'>$i</a></li>";
				}
				$html_page .= "<li class='pageDisable' class='next'><a href=''></a></li>";
				//$html_page .= "<li class='pageDisable'><a href=''>Last</a></li>";
			}
		}
		
		//$html_page .= "<div class='meta-pagination'>Page ". $currentPage .' of '. $numPage ."</div>";
		$html_page .= "</ul></div>";
		return $html_page;
		/*
		<ul>
			<li class="pageDisable"><a href="">First</a></li>
			<li class="pageDisable"><a href="">Previous</a></li>
			<li><a href="">1</a></li>
			<li><a href="">2</a></li>
			<li class="pageActive"><a href="">3</a></li>
			<li class="pageSeparator"><a href="">...</a></li>
			<li><a href="">5</a></li>
			<li><a href="">Next</a></li>
			<li><a href="">Last</a></li>
		</ul>
		*/
	}
    
    public function translate($key_word = null)
    {
        $lang_code = $this->Session->read('lang_code');
        
        App::import("Model", "Translate");  
        $trans = new Translate();  
        $trans->lang_code = $lang_code;
        
        /*
        $translates = array();
        if($this->Session->read('translates')){
            $translates = $this->Session->read('translates');
        }
        else{
            $translates = $trans->get_translate();
            $this->Session->write('translates', $translates);
        }
        */
        
        $translates = $trans->get_translate();
        if(isset($translates[$key_word])){
            echo $translates[$key_word]; 
        }
        else{
            echo $key_word;
        }   
    }
    
    public function get_translate($key_word = null)
    {
        $lang_code = $this->Session->read('lang_code');
        
        App::import("Model", "Translate");  
        $trans = new Translate();  
        $trans->lang_code = $lang_code;
        
        $translates = $trans->get_translate();
        if(isset($translates[$key_word])){
            return $translates[$key_word]; 
        }
        else{
            return $key_word;
        }   
    }          
    
}